package com.example.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;

/**
 * Created by Administrator on 2016/6/21.
 */
@Configuration
public class AmqpConfig {

    public static final String EXCHANGE   = "fanout-exchange";
    public static final String ROUTINGKEY = "queue";
    public static final String QUEUE = "queue";
    public static final String ANOTHER_QUEUE = "another-queue";

    @Bean
    RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        return new RabbitAdmin(connectionFactory);
    }

//    @Bean
//    Queue queue(RabbitAdmin rabbitAdmin){
//        Queue queue = new Queue(QUEUE,true);
//        rabbitAdmin.declareQueue(queue);
//        return queue;
//    }
//
//    @Bean
//    Queue anotherQueue(RabbitAdmin rabbitAdmin){
//        Queue queue = new Queue(ANOTHER_QUEUE,true);
//        rabbitAdmin.declareQueue(queue);
//        return queue;
//    }
//
////    @Bean
////    TopicExchange exchange(RabbitAdmin rabbitAdmin) {
////        TopicExchange topicExchange = new TopicExchange("exchange");
////        rabbitAdmin.declareExchange(topicExchange);
////        return topicExchange;
////    }
//
//    @Bean
//    FanoutExchange exchange(RabbitAdmin rabbitAdmin){
//        FanoutExchange fanoutExchange = new FanoutExchange(EXCHANGE);
//        rabbitAdmin.declareExchange(fanoutExchange);
//        return fanoutExchange;
//    }
//
//    @Bean
//    Binding binding(Queue queue,FanoutExchange exchange,RabbitAdmin rabbitAdmin){
//        Binding binding = BindingBuilder.bind(queue).to(exchange);
//        rabbitAdmin.declareBinding(binding);
//        return binding;
//    }
//
//    @Bean
//    Binding bindingAnother(Queue anotherQueue,FanoutExchange exchange,RabbitAdmin rabbitAdmin){
//        Binding binding = BindingBuilder.bind(anotherQueue).to(exchange);
//        rabbitAdmin.declareBinding(binding);
//        return binding;
//    }

    /**
     * 生产者用
     * @return
     */
    @Bean
    public RabbitMessagingTemplate rabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
        RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
        rabbitMessagingTemplate.setMessageConverter(jackson2Converter());
        rabbitMessagingTemplate.setRabbitTemplate(rabbitTemplate);
        return rabbitMessagingTemplate;
    }

    @Bean
    public MappingJackson2MessageConverter jackson2Converter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        return converter;
    }

}
